<template>
    <form @submit.prevent="handleSubmit">
        <slot></slot>
    </form>
</template>

<script>
const createErrorObj = (model) => {
    let errObj = {}
    Object.keys(model).forEach(key => {
        errObj[key] = ''
    })
    return errObj
}
import eventBus from './event-bus'
export default {
    name: 'Form',
    props:{ 
        rules:Object,
        model: Object
    },
    provide(){
        return {
            errObj: this.errObj
        }
    },
    data(){
        return {
            errObj: createErrorObj(this.model)
        }
    },
    computed:{
        
    },
    methods:{
        validate(){
            return new Promise(async (res,rej) => {
                res()
            })
        },
        handleSubmit(e){
            e.preventDefault()
            this.validate().then(() => {
                this.$emit('onSubmit')
            }).catch(err => {
                console.log(err);
            })
        },
        change(){
            const errObj = createErrorObj(this.model)
            errObj.mobile = '手机号已存在'
            this.errObj = errObj
        }
    },
    mounted(){
    }
}
</script>